package com.app.service.impl;

import com.app.common.Result;
import com.app.mapper.BankMapper;
import com.app.mapper.BusinessMapper;
import com.app.mapper.UserMapper;
import com.app.model.BusinessModel;
import com.app.model.SecuritiesTradingZone;
import com.app.mapper.SecuritiesTradingZoneMapper;
import com.app.model.UserModel;
import com.app.service.ISecuritiesTradingZoneService;
import com.app.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 售劵专区表 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-09-03
 */
@Service
public class SecuritiesTradingZoneServiceImpl extends ServiceImpl<SecuritiesTradingZoneMapper, SecuritiesTradingZone> implements ISecuritiesTradingZoneService {

    @Autowired
    private SecuritiesTradingZoneMapper securitiesTradingZoneMapper;

    @Autowired
    private BusinessMapper businessMapper;


    @Override
    public Result getListWithoutUserId(Integer page, Integer limit) {
        Page p = new Page(page, limit);
        IPage<SecuritiesTradingZone> selectPage = securitiesTradingZoneMapper.getListWithoutUserId(p);
        List<SecuritiesTradingZone> records = selectPage.getRecords();
        PageVo pageVo = new PageVo();
        pageVo.setList(records);
        pageVo.setCount(selectPage.getTotal());
        return Result.success(pageVo);
    }

    @Override
    public Result getPayById(Integer id) {
        SecuritiesTradingZone securitiesTradingZone = securitiesTradingZoneMapper.selectById(id);
        return Result.success(securitiesTradingZone);
    }

    @Override
    public Result getCurrentUserList(Integer userId, Integer page, Integer limit) {
        Page p = new Page(page, limit);
        IPage<SecuritiesTradingZone> securitiesTradingZoneIPage=this.securitiesTradingZoneMapper.getListByUserId(p,userId);
        PageVo pageVo = new PageVo();
        pageVo.setList(securitiesTradingZoneIPage.getRecords());
        pageVo.setCount(securitiesTradingZoneIPage.getTotal());
        return Result.success(pageVo);
    }

    @Override
    public Result resetStatus(Integer id, Integer status) {
        SecuritiesTradingZone securitiesTradingZone = new SecuritiesTradingZone();
        securitiesTradingZone.setStatus(status);
        securitiesTradingZone.setId(id);
        securitiesTradingZoneMapper.updateById(securitiesTradingZone);
        return Result.success("操作成功");
    }

    @Override
    public Result addSecuritiesTradingZone(UserModel userModel, SecuritiesTradingZone securitiesTradingZone) {
        if (userModel.getStatus()!=2) {
            return Result.error(201,"非商家用户不能挂售劵");
        }
        if(securitiesTradingZone.getAmount()>userModel.getVoucher()){
            return Result.error(201,"超出用户剩余益联券");
        }
        if(securitiesTradingZone.getPayType()==null)
        {
            return Result.error(201,"请选择付款方式");
        }

        QueryWrapper<SecuritiesTradingZone> securitiesTradingZoneQueryWrapper=new QueryWrapper<>();
        securitiesTradingZoneQueryWrapper.eq("user_id",userModel.getId());
        securitiesTradingZoneQueryWrapper.eq("status",1);
        if(this.securitiesTradingZoneMapper.selectCount(securitiesTradingZoneQueryWrapper)>0)
        {
            return Result.error(201,"您有尚未售罄的券,请勿再次挂售");
        }
        //根据用户手机号查询商家表得主键id

        BusinessModel businessModel=this.businessMapper.selectByUserPhone(userModel.getPhone());
        if(businessModel==null)
        {
            return Result.error(201,"非商家用户不能挂售劵");
        }
        securitiesTradingZone.setBusinessId(businessModel.getId());
        securitiesTradingZone.setUserId(userModel.getId());
        securitiesTradingZone.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        securitiesTradingZone.setStatus(1);
        securitiesTradingZoneMapper.insert(securitiesTradingZone);
        return Result.success("success");
    }






    /**
     * 管理员列表
     *
     * @param page         第页
     * @param limit        限制
     * @param status       状态
     * @param businessName 企业名称
     * @return {@link IPage}<{@link SecuritiesTradingZone}>
     */
    @Override
    public IPage<SecuritiesTradingZone> adminList(Integer page, Integer limit, Integer status, String businessName) {
        Page p=new Page(page,limit);
        return this.securitiesTradingZoneMapper.adminList(p,status,businessName);
    }

    @Override
    public SecuritiesTradingZone details(Integer id) {

        return this.securitiesTradingZoneMapper.details(id);
    }
}
